#!/bin/bash

. $OPENARS_HOME/common/debug.h
. $OPENARS_HOME/common/functions.h

DEBUG_LEVEL=2

conf_users="$OPENARS_HOME/conf/users.xml"
conf_scripts="$OPENARS_HOME/conf/scripts.xml"
MYLOG=$_LOGFILE

if [ ! -f $conf_users -o ! -f $conf_scripts ]; then
    ERR "not find $conf_users or $conf_scripts"
    exit 1;
fi

format_user > $TEMPDIR/conf_users.tmp

while read line
do
    script_name=`echo $line | sed -n -e '/^\ *<script\ /p' | get_xmlparam "sname"`
    if [ ! -z "$script_name" ]; then
    while read line
    do
	element=`echo $line | sed -n -e 's/\ *<\([^ >]*\).*/\1/p'`
	if [ "$element" == "/script" ]; then
	    break
	elif [ "$element" == "cron" ]; then
	    continue
	elif [ "$element" != "notice" ]; then
	    WARN "unknown xml-element: $element"
	    continue
	fi    

	#element=notice
	user=""
	tools=""
	interval=""
	mail=""
	mobile=""
	type=""
	notice_line=$line
	user=`echo $notice_line | get_xmlparam "user"`
	tools=`echo $notice_line | get_xmlparam "tools"`
	interval=`echo $notice_line | get_xmlparam "interval"`
	type=`echo $notice_line | get_xmlparam "type"`
	echo $tools | grep mail > /dev/null
	[ $? -eq 0 ] && mail=`cat $TEMPDIR/conf_users.tmp | grep "$user" | get_xmlparam "mail"`
	echo $tools | grep mobile > /dev/null
	[ $? -eq 0 ] && mobile=`cat $TEMPDIR/conf_users.tmp | grep "$user" | get_xmlparam "mobile"`
	[ -z "$interval" ] && interval=600 
	[ -z "$type" ] && type="ERR"
	$OPENARS_HOME/tools/log_parser.pl --script_name=$script_name --type=$type --time_interval=$interval $MYLOG > $TEMPDIR/logs.tmp
	if [ -s $TEMPDIR/logs.tmp ]; then
	    if [ ! -z "$mobile" ]; then
		mobile=`echo $mobile | sed s/,/\ /g`
		for i in $mobile; do
		 /etc/zabbix/scripts/sms_itpss.sh $i check "$type in $script_name" > /dev/null
		 INF "send SMS to $user ($i): $type in $script_name"
		done
	    fi
	    if [ ! -z "$mail" ]; then
		> $TEMPDIR/report.tmp
		> $TEMPDIR/breport.tmp
		echo $type | grep REPORT > /dev/null
		if [ $? -eq 0 ]; then
		    report="";
		    rep_files=`cat $TEMPDIR/logs.tmp | awk '$3 == "REPORT" {print $NF}' | sed s/\"//g`
		    echo -e "\n`date +'%Y.%m.%d %H:%M:%S'`" >> $TEMPDIR/report.tmp
		    for i in $rep_files
		    do
			[ ! -f "$i" ] && ERR "not find report-file $i"
			echo -e "\n-----------------------------" >> $TEMPDIR/report.tmp
			cat $i >> $TEMPDIR/report.tmp
		    done
		    breport="";
		    brep_files=`cat $TEMPDIR/logs.tmp | awk '$3 == "BREPORT" {print $NF}' | sed s/\"//g`
		    [ ! -z "$brep_files" ] && echo -e "\n`date +'%Y.%m.%d %H:%M:%S'`\n-----------------------------" >> $TEMPDIR/breport.tmp
		    for i in $brep_files
		    do
			[ ! -f "$i" ] && ERR "not find report-file $i" 
			echo '~< ! '"uuencode $i `basename $i`" >> $TEMPDIR/breport.tmp
		    done

		fi
		cat $TEMPDIR/logs.tmp | awk '$3 !~ /REPORT/' > $TEMPDIR/logs1.tmp
		log_txt=`cat $TEMPDIR/logs1.tmp  $TEMPDIR/report.tmp $TEMPDIR/breport.tmp 2>/dev/null`
mailx -s "$HOSTNAME scripts notice: $script_name" $mail <<-EOF
$type in $script_name:

$log_txt		

EOF
		INF "send mail to $mail: $type in $script_name"

	    fi	
	fi
	#echo "user=$user"
	#echo "tools=$tools"
	#echo "interval=$interval"
	#echo "mail=$mail"
	#echo "mobile=$mobile"
    done
    fi
done < $conf_scripts  

exit 0;
